(function () {
    $.extend($.fn, {
        mask: function ( options) {
            this.unmask();
            // 参数 
            var settings = {
                opacity: 0.4
                , z: 1000
                , bgcolor: '#000'
                , maskall: false
                , msg: false
                , maskDivClass:false
            };
            var op = $.extend(settings, options);
            var original = $(document.body);
            var position = {
                top: 0
                , left: 0
            };
            if (this[0] && this[0] !== window.document) {
                original = this;
                position = original.position();
            }
            // 创建一个 Mask 层，追加到对象中 
            var maskDiv = $('<div class="maskdivgen"> </div>');
            maskDiv.appendTo(original);
            var maskWidth = original.outerWidth();
            if (!maskWidth) {
                maskWidth = original.width();
            }
            var maskHeight = original.outerHeight();
            if (!maskHeight) {
                maskHeight = original.height();
            }
            if (op.maskall) {
                maskWidth = $(window).width()
                maskHeight=$(window).height()
            }
            maskDiv.css({
                position: 'absolute'
                , top: position.top
                , left: position.left
                , 'z-index': op.z
                , width: maskWidth
                , height: maskHeight
                , 'background-color': op.bgcolor
                , opacity: 0
            });
            $(window).resize(function () {
                if (!maskWidth) {
                    maskWidth = original.width();
                }
                var maskHeight = original.outerHeight();
                if (!maskHeight) {
                    maskHeight = original.height();
                }
                if (op.maskall) {
                    maskWidth = $(window).width()
                    maskHeight = $(window).height()
                }
                if (this[0] && this[0] !== window.document) {
                    original = this;
                    position = original.position();
                }
                maskDiv.css({
                    top: original.position().top
                    , left: original.position().left
                    , width: maskWidth
                    , height: maskHeight
                });
            });
            if (op.maskDivClass) {
                maskDiv.addClass(op.maskDivClass);
            }
            if (op.msg) {
                var msgDiv = $('<div style="position:absolute;border:#6593cf 1px solid; padding:2px;background:#ccca"><div style="line-height:24px;border:#a3bad9 1px solid;background:white;padding:2px 10px 2px 10px">' + op.msg + '</div></div>');
                msgDiv.appendTo(maskDiv);
                var widthspace = (maskDiv.width() - msgDiv.width());
                var heightspace = (maskDiv.height() - msgDiv.height());
                msgDiv.css({
                    cursor: 'wait'
                    , top: (heightspace / 2 - 2)
                    , left: (widthspace / 2 - 2)

                });
            }
            maskDiv.fadeIn(
                'fast'
                , function () {
                    // 淡入淡出效果 
                    $(this).fadeTo('slow', op.opacity);

                }
            )
            return maskDiv;
        },
        unmask: function () {
            var original = $(document.body);
            if (this[0] && this[0] !== window.document) {
                original = $(this[0]);
            }
            original.find("> div.maskdivgen").fadeOut('slow', 0,
            function () {
                $(this).remove();
            });
        }
    });
})();